package com.group02.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.annotation.Log;
import com.group02.community.enmus.BusinessType;
import com.group02.community.entity.dict.SysDictData;
import com.group02.community.entity.dict.SysDictType;
import com.group02.community.service.SysDictDataService;
import com.group02.community.service.SysDictTypeService;
import com.group02.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author xl
 * @since 2022-07-01
 */
@RestController
@RequestMapping("/intelligence_community/sys-dict-data")
@Api
@CrossOrigin
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PostMapping("page")
    @ApiOperation("字典数据分页")
    public ResponseEntity<IPage<SysDictData>> selectAllDictData(Long current, Long size, String dict_type, SysDictData sysDictData){
        IPage<SysDictData> dictTypeIPage = sysDictDataService.selectAllDictDatas(current,size,dict_type,sysDictData);
        return ResponseEntity.ok(dictTypeIPage);
    }

    @GetMapping("typeOptions")
    @ApiOperation("获得所有字典名称")
    public ResponseEntity<List<SysDictType>> typeOptions(){
        List<SysDictType> list = sysDictTypeService.list(null);
        return ResponseEntity.ok(list);
    }

    @PostMapping("add")
    @ApiOperation("字典数据新增")
    @Log(title = "字典数据",businessType = BusinessType.INSERT)
    public ResponseEntity<String> addDictData(SysDictData sysDictData){
        sysDictDataService.save(sysDictData);
        return ResponseEntity.ok("新增成功");
    }

    @PutMapping("update")
    @ApiOperation("字典数据修改")
    @Log(title = "字典数据",businessType = BusinessType.UPDATE)
    public ResponseEntity<String> updateDictData(@RequestBody SysDictData sysDictData){
        sysDictDataService.updateById(sysDictData);
        return ResponseEntity.ok("修改成功");
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation("字典数据删除")
    @Log(title = "字典数据",businessType = BusinessType.DELETE)
    public ResponseEntity<String> deleteDictData(@PathVariable Long id){
        sysDictDataService.removeById(id);
        return ResponseEntity.ok("删除成功");
    }

    @DeleteMapping("deleteSelect/{id}")
    @ApiOperation("字典数据批量删除")
    @Log(title = "字典数据",businessType = BusinessType.DELETE)
    public ResponseEntity<String> deleteSelectDictData(@PathVariable String id){
        sysDictDataService.deleteSelectDictData(id);
        return ResponseEntity.ok("删除成功");
    }

    @GetMapping("downloadExcel")
    @ApiOperation("导出数据")
    @Log(title = "字典数据",businessType = BusinessType.EXPORT)
    public ResponseEntity<String> downloadExcel(HttpServletResponse response) throws IOException {
        sysDictDataService.downloadExcel(response);
        return ResponseEntity.ok("导出成功");
    }

    @ApiOperation("获取房间的所有状态")
    @GetMapping("/roomStatus")
    public ResponseResult<List<SysDictData>> getAllRoomStatus() {

      return  ResponseResult.ok(sysDictDataService.getAllRoomStatus()) ;

    }


    @ApiOperation("获取是否商铺")
    @GetMapping("/isShop")
    public ResponseResult<List<SysDictData>> getAllIsShop() {

        return  ResponseResult.ok(sysDictDataService.getAllIsShop()) ;

    }
    @ApiOperation("获取房屋类型")
    @GetMapping("/allRoomType")
    public ResponseResult<List<SysDictData>> getAllRoomType() {

        return  ResponseResult.ok(sysDictDataService.getAllRoomType()) ;

    }
    @GetMapping("/elevator")
    public ResponseResult<List<SysDictData>> getAllElevator() {

        return  ResponseResult.ok(sysDictDataService.getAllElevator()) ;

    }
}

